import { AssetDao } from './../dao/assetDao'
import { Asset } from '../model/asset'
import { LiabilitiesDao } from './../dao/liabilitiesDao'
import { BorrowingDao } from './../dao/borrowingDao'
import { IncomeDao } from './../dao/incomeDao'
import { IamInfo } from '../controller/iamInfo'
import { ResponseList } from './../model/responseList'
export class AssetService {
  countAssetByMonth(params: any):Promise<number> {
    params.creator = IamInfo.id
    return this.assetDao.count(params)
  }
  private readonly assetDao = new AssetDao();
  queryFundsList(params: any): Promise<ResponseList<Asset>> {
    params.creator = IamInfo.id
    return this.assetDao.queryFundsList(params).then(modelData => {
      return this.assetDao.count(params).then(count => {
        const responseList = new ResponseList<Asset>();
        responseList.list = modelData
        responseList.count = count
        return responseList
      })
    })
  }
  queryAsset(params: any): Promise<Asset> {
    params.creator = IamInfo.id
    return this.assetDao.queryAsset(params)
  }
  upsertAsset(params: any): Promise<[Asset, boolean]> {
    params.creator = IamInfo.id
    params.updater = IamInfo.id
    return this.assetDao.upsertAsset(params)
  }
  deleteAsset(params: any): Promise<number> {
    params.creator = IamInfo.id
    return this.assetDao.deleteAsset(params)
  }
  queryNetAssets(params: any): Promise<any> {
    params.creator = IamInfo.id
    return this.assetDao.queryFundsListByDateRange(params).then(assets => {
      return new LiabilitiesDao().queryLiabilityListByDateRange(params).then(liabilitys => {
        return new BorrowingDao().queryArrearsListByDateRange(params).then(owed => {
          return new IncomeDao().queryIncomeListByDateRange(params).then(incomeList => {
            // 获取月份数据汇总做加减
            const netAssets = []
            const endDate = new Date(params.endDate)
            const startDate = new Date(params.startDate)
            for (; startDate < endDate; startDate.setMonth(startDate.getMonth() + 1)) {
              const currentMonthAssets = this.getMonthAssetSum(assets, startDate)
              const currentMonthLiabilitys = this.getUnrepaidData(liabilitys, startDate)
              const currentMonthOwed = this.getUnrepaidData(owed, startDate)
              const currentMonthIncome = this.getMonthAssetSum(incomeList, startDate)
              netAssets.push({
                dateMonth: this.converToDateMonth(startDate),
                netAsset: (currentMonthAssets - currentMonthLiabilitys + currentMonthOwed).toFixed(2),
                liability: currentMonthLiabilitys.toFixed(2),
                borrowing: currentMonthOwed.toFixed(2),
                income: currentMonthIncome.toFixed(2)
              })
            }
            return netAssets
          })
        })
      })
    })
  }
  private getMonthAssetSum(rangedData: Array<any>, date: Date): number {
    let currentMonthSum = 0
    const monthStartDateTime = date.getTime()
    const monthEndDateTime = new Date(date.getFullYear(), date.getMonth() + 1, 1).getTime()
    for (let j = 0; j < rangedData.length; j++) {
      const monthItem = rangedData[j]
      const monthDateTime = new Date(monthItem.date).getTime()
      if (monthStartDateTime <= monthDateTime && monthDateTime < monthEndDateTime) {
        currentMonthSum += monthItem.amount
      }
    }
    return currentMonthSum
  }
  private getUnrepaidData(rangedData: Array<any>, date: Date): number {
    let currentMonthSum = 0
    const monthEndDateTime = new Date(date.getFullYear(), date.getMonth() + 1, 1).getTime()
    const rangeDataLen = rangedData.length
    for (let j = 0; j < rangeDataLen; j++) {
      const monthItem = rangedData[j]
      if (new Date(monthItem.date).getTime() < monthEndDateTime && (!monthItem.actualRepaymentDate || new Date(monthItem.actualRepaymentDate).getTime() >= monthEndDateTime)) {
        // 没有还款日期或还款日期大于当前月份都被计入当月负债
        currentMonthSum += monthItem.amount
      }
    }
    return currentMonthSum
  }
  private converToDateMonth(date: Date): string {
    const year = date.getFullYear()
    const month = date.getMonth() > 8 ? date.getMonth() + 1 : '0' + (date.getMonth() + 1)
    return year + '-' + month
  }
}
